#import "sprite_library_experiment_dialog.h"

@implementation sprite_library_experiment_dialog

- (id)initSpriteLibraryExperimentPreViewToturial
{
	int i;
	CCLabelTTF *_Label;
	std::vector<CCLabelTTF *> _Table;
	float _TotalY;
	float _StartY;

	if ((self = [super init]))
	{
		if (g_FlagChinese)
		{
			_Label = [CCLabelTTF labelWithString:@"将碎片拖入心性容器," fontName:@"ArialMT" fontSize:LIBRARY_EXPRIMENT_TUTORIAL_FONT_SIZE];
			[self addChild:_Label];
			_Label.anchorPoint = ccp(0.5f, 1.0f);
			_Table.push_back(_Label);
			_Label = [CCLabelTTF labelWithString:@"然后摇动你的设备来合成," fontName:@"ArialMT" fontSize:LIBRARY_EXPRIMENT_TUTORIAL_FONT_SIZE];
			[self addChild:_Label];
			_Label.anchorPoint = ccp(0.5f, 1.0f);
			_Table.push_back(_Label);
			_Label = [CCLabelTTF labelWithString:@"或者点击“shake”按钮也可以." fontName:@"ArialMT" fontSize:LIBRARY_EXPRIMENT_TUTORIAL_FONT_SIZE];
			[self addChild:_Label];
			_Label.anchorPoint = ccp(0.5f, 1.0f);
			_Table.push_back(_Label);
		}
		else
		{
			_Label = [CCLabelTTF labelWithString:@"Drag two fragments of love" fontName:@"ArialMT" fontSize:LIBRARY_EXPRIMENT_TUTORIAL_FONT_SIZE];
			[self addChild:_Label];
			_Label.anchorPoint = ccp(0.5f, 1.0f);
			_Table.push_back(_Label);
			_Label = [CCLabelTTF labelWithString:@"into the heart shaped container," fontName:@"ArialMT" fontSize:LIBRARY_EXPRIMENT_TUTORIAL_FONT_SIZE];
			[self addChild:_Label];
			_Label.anchorPoint = ccp(0.5f, 1.0f);
			_Table.push_back(_Label);
			_Label = [CCLabelTTF labelWithString:@"and then shake your phone" fontName:@"ArialMT" fontSize:LIBRARY_EXPRIMENT_TUTORIAL_FONT_SIZE];
			[self addChild:_Label];
			_Label.anchorPoint = ccp(0.5f, 1.0f);
			_Table.push_back(_Label);
			_Label = [CCLabelTTF labelWithString:@"to generate new items!" fontName:@"ArialMT" fontSize:LIBRARY_EXPRIMENT_TUTORIAL_FONT_SIZE];
			[self addChild:_Label];
			_Label.anchorPoint = ccp(0.5f, 1.0f);
			_Table.push_back(_Label);
			_Label = [CCLabelTTF labelWithString:@"Also you can just click the “shake” button!" fontName:@"ArialMT" fontSize:LIBRARY_EXPRIMENT_TUTORIAL_FONT_SIZE];
			[self addChild:_Label];
			_Label.anchorPoint = ccp(0.5f, 1.0f);
			_Table.push_back(_Label);
		}

		_TotalY = _Table.size() * _Table[0].contentSizeInPixels.height + (_Table.size() - 1) * 2;
		_StartY = _TotalY / 2;

		for (i = 0; i < _Table.size(); i++)
		{
			_Table[i].positionInPixels = ccp(0.0f, _StartY);
			_StartY -= _Table[0].contentSizeInPixels.height + 2;
		}

		coverRect_.center = CGPointZero;
		coverRect_.size = CGSizeMake(g_ScrrenSize.width, _TotalY + 200.0f);

		[self ActionOpen];
	}
	return self;
}

- (id)initSpriteLibraryExperimentPreView:(int)Type And:(int)Id And:(int)Count
{
	NSString *_ImagePath;
	NSString *_Text;
	CCSprite *_ImageObj;
	CCLabelTTF *_TextObj;
	float _TotalW;

	if ((self = [super init]))
	{
		switch (Type)
		{
		case ID_TYPE_WEAPON:
			_ImagePath = [NSString stringWithUTF8String:g_CfgWeaponTable[Id].icon_path];
			if (g_FlagChinese)
			{
				_Text = [@"武器 " stringByAppendingString:[NSString stringWithUTF8String:g_CfgWeaponTable[Id].name]];
				_Text = [_Text stringByAppendingString:@"解锁"];
			}
			else
			{
				_Text = [NSString stringWithFormat:@"Weapon %s Unlock", g_CfgWeaponTable[Id].name];
			}
			break;
		case ID_TYPE_ITEM:
			_ImagePath = [NSString stringWithUTF8String:g_CfgItemTable[Id].icon_path];
			if (g_RecordItemLock[Id])
			{
				if (g_FlagChinese)
				{
					_Text = [@"道具 " stringByAppendingString:[NSString stringWithUTF8String:g_CfgItemTable[Id].name]];
					_Text = [_Text stringByAppendingString:@"解锁"];
				}
				else
				{
					_Text = [NSString stringWithFormat:@"Item %s Unlock x%d", g_CfgItemTable[Id].name, Count];
				}
				g_RecordItemLock[Id] = false;
			}
			else if (g_FlagChinese)
			{
				_Text = [@"道具 " stringByAppendingString:[NSString stringWithUTF8String:g_CfgItemTable[Id].name]];
				_Text = [_Text stringByAppendingFormat:@" 生成 %d", Count];
			}
			else
			{
				_Text = [NSString stringWithFormat:@"Item %s Create x%d", g_CfgItemTable[Id].name, Count];
			}
			break;
		case ID_TYPE_EXHIBITION:
			switch (g_CfgExhibitionElementTable[Id].type)
			{
			case EXHIBITION_ELEMENT_TYPE_MUSIC: _ImagePath = TREASURE_ICON_MUSIC_PATH; break;
			case EXHIBITION_ELEMENT_TYPE_TEXT: _ImagePath = TREASURE_ICON_TEXT_PATH; break;
			case EXHIBITION_ELEMENT_TYPE_PICTURE: _ImagePath = TREASURE_ICON_IMAGE_PATH; break;
			default:;
			}
			if (g_FlagChinese)
			{
				_Text = [@"收藏 " stringByAppendingString:[NSString stringWithUTF8String:g_CfgExhibitionElementTable[Id].name]];
				_Text = [_Text stringByAppendingString:@" 解锁"];
			}
			else
			{
				_Text = [NSString stringWithFormat:@"exhibition %s Unlock", g_CfgExhibitionElementTable[Id].name];
			}
			break;
		default:;
		}
		_ImageObj = [CCSprite spriteWithSpriteFrameName:_ImagePath];
		_TextObj = [CCLabelTTF labelWithString:_Text fontName:@"ArialMT" fontSize:LIBRARY_EXPRIMENT_ITEM_FONT_SIZE];
		[self addChild:_ImageObj];
		[self addChild:_TextObj];
		_ImageObj.anchorPoint = ccp(0.0f, 0.5f);
		_TextObj.anchorPoint = ccp(0.0f, 0.5f);
		_TotalW = _ImageObj.contentSizeInPixels.width + _TextObj.contentSizeInPixels.width + 20.0f;
		_ImageObj.positionInPixels = ccp(-_TotalW / 2.0f, 0.0f);
		_TextObj.positionInPixels = ccp(-_TotalW / 2.0f + _ImageObj.contentSizeInPixels.width + 20.0f, 0.0f);
		coverRect_.center = CGPointZero;
		coverRect_.size = CGSizeMake(g_ScrrenSize.width, 200.0f);
		[self ActionOpen];
	}
	return self;
}

- (void)draw
{
	glColor4f(0.0f, 0.0f, 0.0f, 0.8f);
	uiRenderRect(coverRect_);
}

- (void)ActionOpen
{
	CCSequence *_Seq;
	id _ActionOpen;
	CCCallFuncN *_CallBackOpenOver;

	self.scaleX = 1.0f;
	self.scaleY = 0.0f;
	_ActionOpen = [CCScaleTo actionWithDuration:COVER_OPEN_TIME scaleX:1.0f scaleY:1.0f];
	_CallBackOpenOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionOpenOver:)];
	_Seq = [CCSequence actions:_ActionOpen, _CallBackOpenOver, nil];
	[self runAction:_Seq];

	g_LayerLibraryExperiment.flagTouchLock = true;
}

- (void)ActionClose
{
	CCSequence *_Seq;
	id _ActionClose;
	CCCallFuncN *_CallBackCloseOver;

	self.scaleX = 1.0f;
	self.scaleY = 1.0f;
	_ActionClose = [CCScaleTo actionWithDuration:COVER_CLOSE_TIME scaleX:1.0f scaleY:0.0f];
	_CallBackCloseOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionCloseOver:)];
	_Seq = [CCSequence actions:_ActionClose, _CallBackCloseOver, nil];
	[self runAction:_Seq];

	g_LayerLibraryExperiment.flagTouchLock = true;
}

- (void)CallBackActionOpenOver:(id)Sender
{
	/*effect
	*/
	EffectItemMake();

	g_LayerLibraryExperiment.flagTouchLock = false;
}

- (void)CallBackActionCloseOver:(id)Sender
{
	g_LayerLibraryExperiment.flagTouchLock = false;
	[g_LayerLibraryExperiment PreviewOver];
}

@end